package com.example.bishe.toolclass;

import com.alibaba.fastjson.JSONObject;
import com.example.bishe.entity.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.*;


/**
 * @Author yws
 * @Date 2022/3/9 16:40
 */
@Component
public class JsonValue {//获取JSON文档中对应的Key的值value
    @Resource
    StringTurnJson stringTurnJson;

    public JsonValue() {
    }

    public String getValue(JSONObject json, String Key) {//获取对应key的value值
        JSONObject jsonObject = stringTurnJson.getJson(String.valueOf(json));
        String value = (String) jsonObject.get(Key);
        return value;
    }

    public HashMap<String, String> getValue(JSONObject json, String... keys) {//返回JSON中的多个键值对数据
        JSONObject jsonObject = stringTurnJson.getJson(String.valueOf(json));
        //String value = (String) jsonObject.get(Key);
        HashMap<String, String> hashMap = new HashMap<>();
        for (String x : keys) {
            hashMap.put(x,jsonObject.get(x)+"");
        }
        return hashMap;
    }

    public List<Tag> getValue(JSONObject json) {//返回JSON中的多个键值对数据,用于标签的使用
        /**
         * Map<String, Class<ViewNameEntity>> classMap = new HashMap<String, Class<ViewNameEntity>>();
         * classMap.put("ViewNameEntity", ViewNameEntity.class);
         * List<ViewNameEntity> viewNameList = JSONArray.toList(ja, ViewNameEntity.class, classMap);
         * */
        JSONObject jsonObject = stringTurnJson.getJson(String.valueOf(json));
        List list = Collections.singletonList(jsonObject.get("taglist"));
        List<Tag> listTag = new ArrayList<>();

        for (int i = 0; i < list.size(); i++) {
            String x = (String) list.get(i);
            String tagname = (String) stringTurnJson.getJson(x).get("tagname");
            String tagid = (String) stringTurnJson.getJson(x).get("tagid");
            listTag.add(new Tag(tagid, tagname));
        }
        return listTag;
    }
}
